//
//  JSMScrollViewPackage.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMScrollViewPackage.h"
#import <UIImageView+WebCache.h>

@interface JSMScrollViewPackage ()<UIScrollViewDelegate>

@end
@implementation JSMScrollViewPackage
- (id)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.imagesArr = imagesArray;
        self.imagesNumber = self.imagesArr.count;
        [self createView];
        
    }
    return self;
}

- (void)createView {
    self.scrollView = [[BaseScrollView alloc] initWithFrame:self.frame];
    self.scrollView.contentSize = CGSizeMake((self.imagesArr.count + 2) * WIDTH, 0);
    
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    [self addSubview:self.scrollView];
    // 水平
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    // 垂直
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    // 页面控制 pageController

    self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 100, HEIGHT - 50, 200, 50)];
    [self addSubview:self.pageController];
    self.pageController.numberOfPages = self.self.imagesArr.count;
    self.pageController.pageIndicatorTintColor = [UIColor grayColor];
    [[self.scrollView superview] addSubview:self.pageController];
    
    // 定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(nextImage:) userInfo:nil repeats:YES];
    // 点击方法
    UITapGestureRecognizer *tapNextPage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(detailPage:)];
    [self.scrollView addGestureRecognizer:tapNextPage];
    
    // 轮播图
    UIImageView *firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
           [firstImageView sd_setImageWithURL:[NSURL URLWithString:self.imagesArr[self.imagesNumber - 1]] placeholderImage:PlaceHolderImage];
       [self.scrollView addSubview:firstImageView];
    
    for (NSInteger i = 0; i < self.imagesNumber; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i + 1) * WIDTH, 0, WIDTH, HEIGHT)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imagesArr[i]] placeholderImage:PlaceHolderImage];
       [self.scrollView addSubview:imageView];
        
    }
    
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * (self.imagesNumber + 1), 0, WIDTH, HEIGHT)];
            [lastImageView sd_setImageWithURL:[NSURL URLWithString:self.imagesArr[0]] placeholderImage:PlaceHolderImage];
    
    [self.scrollView addSubview:lastImageView];
}

#pragma mark 通过NSTimer 来让scrollView进行无限滚动
- (void)nextImage:(NSTimer *)timer {
    self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + WIDTH, 0);
    if (self.scrollView.contentOffset.x == WIDTH * (self.imagesNumber + 1)) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(WIDTH * self.imagesNumber, 0);
    }
    self.pageController.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
}

#pragma mark 当滑动结束,稳定之后才会被触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(WIDTH * self.imagesNumber, 0);
    } else if (self.scrollView.contentOffset.x == WIDTH * (self.imagesNumber + 1)) {
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.pageController.currentPage = self.scrollView.contentOffset.x / WIDTH - 1;
}

#pragma mark 点击手势方法
- (void)detailPage:(UITapGestureRecognizer *)tap {
    NSInteger i = 0;
    for (; i < self.imagesNumber; i++) {
        if (self.scrollView.contentOffset.x == WIDTH * (i + 1)) {
            break;
        }
    }
#pragma mark 3.代理人调用协议方法
    if ([self.delegate respondsToSelector:@selector(detailWebViewWithTag:)]) {
        [self.delegate detailWebViewWithTag:i];
    }
}

#pragma mark 定时器的停止与开始
// 当用户准备拖拽时,关闭定时器,并且把定时器设置为nil.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
    self.timer = nil;
}
// 当用户停止拖拽时,重新添加一个定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(nextImage:) userInfo:nil repeats:YES];
    // 为了防止单线程的弊端,可以保证用户在使用其他控件的时候系统照样可以让定时器运转
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}



@end
